Skip to content

fix: close browser after export#4373

Merged
ravern merged 2 commits into
nusmodifications:masterfrom
howenyap:howen/fix-unclosed-browser-in-export-service
May 23, 2026
Merged

fix: close browser after export#4373
ravern merged 2 commits into
nusmodifications:masterfrom
howenyap:howen/fix-unclosed-browser-in-export-service

Conversation

@howenyap
Copy link
Copy Markdown
Contributor

Context

The serverless export service opens a browser but only returns the page handler

Then, cleanup only closes the page, leaving the browser handle open.

Implementation

  • Return both the browser and page handle in the open function
  • Close the browser after the export completes, this closes the page as well
image

Other Information

Disclaimer:
I found this leak with Codex Security and told @ravern about it!

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 24, 2026

@howenyap is attempting to deploy a commit to the modsbot's projects Team on Vercel.

A member of the Team first needs to authorize it.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 56.40%. Comparing base (988c6fd) to head (ce11372).
⚠️ Report is 233 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4373      +/-   ##
==========================================
+ Coverage   54.52%   56.40%   +1.88%     
==========================================
  Files         274      317      +43     
  Lines        6076     6962     +886     
  Branches     1455     1679     +224     
==========================================
+ Hits         3313     3927     +614     
- Misses       2763     3035     +272     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ravern
Copy link
Copy Markdown
Member

ravern commented Mar 24, 2026

@greptile

@howenyap
Copy link
Copy Markdown
Contributor Author

@greptile

first greptile scan kinda nervous

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 24, 2026

Greptile Summary

This PR fixes a browser resource leak in the serverless export service where puppeteer.launch() opened a browser but only the Page handle was returned and eventually closed, leaving the browser process running indefinitely.

  • render-serverless.ts: open() now returns { browser, page } instead of just page, and adds a try/catch to close the browser if page initialization fails mid-setup.
  • handler.ts: Destructures both handles from open(), wraps performExport in a try/finally that calls browser.close() — which closes all pages and the browser process — guaranteeing cleanup even when the export throws.

Confidence Score: 5/5

Safe to merge — the fix correctly closes both the browser and all its pages in every exit path.

The change is minimal and targeted: the open function now returns the browser handle alongside the page, and the finally block ensures browser.close() is always called regardless of whether the export succeeds or throws. The try/catch inside open handles the case where page initialization itself fails, closing the browser before re-throwing. No new logic paths are introduced that could behave unexpectedly.

No files require special attention.

Sequence Diagram

sequenceDiagram
    participant H as handler.ts
    participant R as render-serverless.ts
    participant P as Puppeteer

    H->>R: open(url)
    R->>P: puppeteer.launch()
    P-->>R: browser
    R->>P: browser.newPage()
    P-->>R: page
    R->>P: page.goto(url)
    alt Page setup fails
        R->>P: browser.close()
        R-->>H: throws error
    else Success
        R-->>H: "{ browser, page }"
    end

    H->>H: performExport(response, page, data)
    note over H: try / finally
    H->>P: browser.close()
    note over P: Closes browser + all pages
Loading

Reviews (3): Last reviewed commit: "fix: remove optional chain" | Re-trigger Greptile

Comment thread export/src/handler.ts Outdated
@howenyap howenyap force-pushed the howen/fix-unclosed-browser-in-export-service branch from b79003c to 2bf0914 Compare March 24, 2026 09:08
@jloh02
Copy link
Copy Markdown
Member

jloh02 commented May 18, 2026

@greptile

@jloh02 jloh02 self-assigned this May 18, 2026
@vercel
Copy link
Copy Markdown

vercel Bot commented May 21, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated (UTC)
nusmods-export Ignored Ignored Preview May 21, 2026 4:43am
nusmods-website Ignored Ignored Preview May 21, 2026 4:43am

Request Review

@ravern ravern force-pushed the howen/fix-unclosed-browser-in-export-service branch from c023fcf to 07a68ec Compare May 23, 2026 04:55
@ravern
Copy link
Copy Markdown
Member

ravern commented May 23, 2026

@greptileai review

@ravern ravern force-pushed the howen/fix-unclosed-browser-in-export-service branch from 07a68ec to ce11372 Compare May 23, 2026 10:32
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 23, 2026

PR author is not in the allowed authors list.

Copy link
Copy Markdown
Member

@ravern ravern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved for squash merge.

@ravern ravern merged commit b1b7a2f into nusmodifications:master May 23, 2026
3 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants